home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 05 - 1989 / 05.08 Aug 89 / XCMD Source / FullPathName.c next >
Encoding:
C/C++ Source or Header  |  1989-06-15  |  3.7 KB  |  150 lines  |  [TEXT/KAHL]

  1. /********************************/
  2. /* File: FullPathName.c            */
  3. /*                                */
  4. /* Given the name and working    */
  5. /* directory of a file, walk the*/
  6. /* directory tree to determine    */
  7. /* what the full pathname of the*/
  8. /* directory is...                */
  9. /*                                */
  10. /* Paramters:                    */
  11. /* param0 = file name    num        */
  12. /* param1 = directory id         */
  13. /*                                */
  14. /* Out:                            */
  15. /* full pathname of the working    */
  16. /* directory                     */
  17. /* ----------------------------    */
  18. /* Once again, I am indepbted to*/
  19. /* Steve Maller of Apple         */
  20. /* Computer Inc for illuminating*/
  21. /* this oft too dark realm of     */
  22. /* the toolbox.                    */
  23. /********************************/
  24.  
  25. #include    <MacTypes.h>
  26. #include    <OSUtil.h>
  27. #include    <MemoryMgr.h>
  28. #include    <FileMgr.h>
  29. #include    <ResourceMgr.h>
  30. #include    <pascal.h>
  31. #include    <strings.h>
  32. #include    <hfs.h>
  33. #include     "HyperXCmd.h"
  34. #include    "HyperUtils.h"
  35.  
  36. #define        nil    0L
  37.  
  38. char        colon[2] = "\p:";
  39.  
  40. void ClimbTree( child, cpb, fullName )
  41.     long        child;
  42.     CInfoPBPtr    cpb;
  43.     char        *fullName;
  44. /*************************
  45. * Climb the directory tree
  46. * until we reach the root
  47. *
  48. * Allocate the records in the
  49. * heap to keep the stack frame
  50. * as small as necessary.  Too
  51. * large a stack frame can 
  52. * lead to a case of terminal
  53. * terminal.
  54. *
  55. * child is the working directory
  56. * id of the "current folder", vol
  57. * is the volume reference number and
  58. * fullName points to the 
  59. * output string
  60. *************************/
  61. {
  62.     StringPtr    folder_name    = (StringPtr)NewPtr( 256 );
  63.     
  64.     /*** setting the file directory index to -1    ***/
  65.     /*** lets us get information about the        ***/
  66.     /*** directory whose id is specified in    the    ***/
  67.     /*** ioDrDIrID field of the parameter block    ***/
  68.     folder_name[0] = '\0';    
  69.     
  70.     cpb->dirInfo.ioNamePtr     = (StringPtr)folder_name;                
  71.     cpb->dirInfo.ioDrDirID     = child; 
  72.  
  73.     if( PBGetCatInfo( cpb, 0) == noErr ){
  74.         ClimbTree(cpb->dirInfo.ioDrParID,cpb,fullName);
  75.  
  76.         Concat( (char *)fullName, (char *)folder_name );
  77.         Concat( (char *)fullName, (char *)&colon );
  78.     }
  79.     DisposPtr( folder_name     );
  80.     
  81. }
  82.  
  83.  
  84. pascal void main( paramPtr )
  85.     XCmdBlockPtr    paramPtr;
  86. {
  87.     Str31            str,fName;
  88.     short            wdid;
  89.     WDPBRec            theWDPB;
  90.     CInfoPBRec        theCPB;
  91.     HParamBlockRec    theHPB;
  92.     char            fullPath[256];
  93.     char            part_Name[256]; /*** used in HPB    ***/
  94.     char            vol_Name[256];
  95.     OSErr            err;
  96.  
  97.     colon[0] = 1;
  98.     colon[1] = ':';
  99.     
  100.     vol_Name[0] = '\0';
  101.     part_Name[0]= '\0';
  102.     /*** empty is the default answer ***/
  103.     paramPtr->returnValue = 0L;
  104.  
  105.     HLock( paramPtr->params[0] );
  106.     ZeroToPas( paramPtr, *(paramPtr->params[0]), &fName );
  107.     HUnlock( paramPtr->params[0] );
  108.     
  109.     /*** convert the wdid to a usable form ***/
  110.     HLock( paramPtr->params[1] );
  111.     ZeroToPas( paramPtr, *(paramPtr->params[1]), &str );
  112.     HUnlock( paramPtr->params[1] );
  113.     wdid = StrToNum( paramPtr, &str );
  114.     
  115.     /*** First, we appeal to GetVInfo to get    ***/
  116.     /*** the volume name that the file lives on    ***/
  117.     part_Name[0]         = '\0';                
  118.     theHPB.volumeParam.ioNamePtr    = (StringPtr)vol_Name;     
  119.     theHPB.volumeParam.ioVRefNum     = (short)wdid;         
  120.     theHPB.volumeParam.ioVolIndex     = 0;                 
  121.     if(PBHGetVInfo( &theHPB, 0) != noErr )
  122.         return;     
  123.  
  124.     /*** Next, use the working directory info    ***/
  125.     /*** to walk the directory tree backwards     ***/
  126.     /*** to the root directory                    ***/
  127.     theWDPB.ioNamePtr    = (StringPtr)part_Name;
  128.     theWDPB.ioVRefNum     = wdid;         
  129.     theWDPB.ioWDProcID    = 0;             
  130.     theWDPB.ioWDIndex     = 0;             
  131.     if( PBGetWDInfo( &theWDPB, 0) != noErr )     
  132.         return;
  133.     
  134.     fullPath[0] = '\0'; 
  135.     theCPB.dirInfo.ioFDirIndex = -1; 
  136.     theCPB.dirInfo.ioVRefNum    = theHPB.volumeParam.ioVRefNum;        
  137.  
  138.     ClimbTree(    theWDPB.ioWDDirID, 
  139.                 (CInfoPBPtr)&theCPB, 
  140.                 (StringPtr)fullPath );
  141.     
  142.     /*** Climbing the tree yields the names of    ***/
  143.     /*** all the folders which we still need to ***/
  144.     /*** add the file's name to.                ***/
  145.     Concat( (char *)fullPath, (char *)&fName );
  146.  
  147.     paramPtr->returnValue = PasToZero( paramPtr, fullPath );
  148. }
  149.  
  150.